package com.auto.admin.controller.backend;


import com.alibaba.fastjson.JSONObject;
import com.auto.admin.controller.backend.vo.param.ExecRequestVO;
import com.auto.admin.controller.backend.vo.ApiInfoVO;
import com.auto.admin.entity.bo.ApiInfoBO;
import com.auto.admin.entity.bo.param.ExecRequestBO;
import com.auto.admin.manager.ApiInfoManager;
import com.auto.api.common.entity.ListData;
import com.auto.api.common.entity.ResultData;
import com.auto.api.common.utils.BeanMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author liangshf
 * @Description: ApiInfoController
 * @date 2019-06-20
 */
@RestController
@RequestMapping("/backend/mockMapping")
@Api(produces = "ApiInfoController接口")
@Slf4j
public class ApiInfoController {

    @Resource
    private ApiInfoManager apiInfoManager;

    @ApiOperation(value = "MockMapping列表")
    @PostMapping(value = "/findList")
    @ResponseBody
    public ResultData<ListData<ApiInfoVO>> findList(@RequestBody(required = false) ApiInfoVO paramVO) {
        if (paramVO == null)
            paramVO = new ApiInfoVO();
        ApiInfoBO paramBO = BeanMapper.map(paramVO, ApiInfoBO.class);
        List<ApiInfoVO> mockList = BeanMapper.mapList(apiInfoManager.findListByParamBO(paramBO), ApiInfoVO.class);
        return ResultData.ok(ListData.newList(mockList));
    }

    @ApiOperation(value = "获取一个url")
    @GetMapping(value = "/getUrl")
    @ResponseBody
    public ResultData<ApiInfoVO> getUrl(@RequestParam Long id) {
        ApiInfoVO mappingVO = BeanMapper.map(apiInfoManager.getById(id), ApiInfoVO.class);
        return ResultData.ok(mappingVO);
    }


    @ApiOperation(value = "执行模拟请求")
    @PostMapping(value = "/call")
    @ResponseBody
    public Object call(@RequestBody @Validated ExecRequestVO paramVO) {
        ExecRequestBO paramBO = BeanMapper.map(paramVO, ExecRequestBO.class);
        String result = apiInfoManager.request(paramBO);
        return JSONObject.toJSON(result);
    }
}

